package com.taotao.manage.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.taotao.common.utils.EasyUIResult;
import com.taotao.manage.pojo.Content;
import com.taotao.manage.service.ContentService;

@RequestMapping("content")
@Controller
public class ContentController {

    @Autowired
    private ContentService contentService;
    
    private static final Logger LOGGER  = LoggerFactory.getLogger(ContentController.class);
    
    /**
     * 新增内容
     * 
     * @param content
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<Void> saveContent(Content content) {
        try {
        	if (LOGGER.isDebugEnabled()) {
				//{}为占位符
				LOGGER.debug("新增内容！ content={}",content);
        	}
            content.setId(null);
            this.contentService.save(content);
            if (LOGGER.isDebugEnabled()) {
				//{}为占位符
				LOGGER.debug("新增内容成功！ content={}",content);
        	}
            return ResponseEntity.status(HttpStatus.CREATED).build();
        } catch (Exception e) {
        	if (LOGGER.isDebugEnabled()) {
				//{}为占位符
				LOGGER.debug("新增内容失败！ content={}",content,e);
        	}
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询内容列表，按照更新时间倒序排序
     * 
     * @param categoryId
     * @param page
     * @param rows
     * @return
     */
   @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<EasyUIResult> queryContentList(@RequestParam("categoryId") Long categoryId,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "10") Integer rows) {
        try {
            EasyUIResult easyUIResult = this.contentService.queryContentListByUpdatedTimeDESC(categoryId, page, rows);
            return ResponseEntity.ok(easyUIResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }
   
   /**
    * 删除内容
    * @param content
    * @return
    */
   @RequestMapping(method = RequestMethod.DELETE)
   public ResponseEntity<Void> deleteContentByIds(@RequestParam("ids") List<Object> ids) {
       try {
    	   if (LOGGER.isDebugEnabled()) {
				//{}为占位符
				LOGGER.debug("删除内容！ ids={}",ids);
       	}
           Integer count = this.contentService.deleteContentByIds(ids);
        	   if (LOGGER.isDebugEnabled()) {
   				//{}为占位符
   				LOGGER.debug("删除内容成功！ ids={},删除数量：count={}",ids,count);
          	}
        	 return ResponseEntity.status(HttpStatus.OK).build();
		 
       } catch (Exception e) {
    	   if (LOGGER.isDebugEnabled()) {
  				//{}为占位符
  				LOGGER.debug("删除内容失败 ids={}",ids,e);
         	}
       }
       return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
   }

}
